﻿using QldtSdh.GUI.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.GUI.Utils;
using QldtSdh.DAL.Models;

namespace QldtSdh.GUI.NCS
{
    public partial class FormQuanLyNganhDaoTao : BaseDialog
    {
        bool reload = false;
        public FormQuanLyNganhDaoTao()
        {
            InitializeComponent();
            gridData.AddRowIndexing();
            InitGrid();
            LoadGrid();
        }

        void InitGrid()
        {
            gridData.AutoGenerateColumns = false;
            colMaNganhDaoTao.DataPropertyName = "MaNganhDaoTao";
            colMaSoNganh.DataPropertyName = "MaSoNganhDaoTao";
            colTenNganh.DataPropertyName = "TenNganhDaoTao";
        }

        void LoadGrid()
        {
            bdData.DataSource = BizChuyenNganhDaoTaoNCS.GetList();
            gridData.DataSource = bdData;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            var current = bdData.Current as NganhDaoTaoNCS;
            if (current != null)
            {
                var frm = new FormQuanLyNganhDaoTao_ChiTiet(current);
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    reload = true;
                    LoadGrid();
                }
            }

        }

        private void bdData_CurrentChanged(object sender, EventArgs e)
        {
            var current = bdData.Current as NganhDaoTaoNCS;
            if (current != null)
            {
                btnSua.Enabled = btnXoa.Enabled = true;
            }
            else
            {
                btnSua.Enabled = btnXoa.Enabled = false;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            var frm = new FormQuanLyNganhDaoTao_ChiTiet();
            frm.OnChanged += (o, t) =>
            {
                reload = true;
                LoadGrid();
            };
            frm.ShowDialog();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            var current = bdData.Current as NganhDaoTaoNCS;
            if (current != null)
            {
                if (DialogUtils.ShowOKCancel(NOTIFY_CONSTANT_STRING.DELETE) == System.Windows.Forms.DialogResult.OK)
                {
                    var rs = BizChuyenNganhDaoTaoNCS.Delete(current.MaNganhDaoTaoNCSGUID);
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {
                        reload = true;
                        bdData.RemoveCurrent();
                    }
                    else if (rs == CODE_RESULT_RETURN.ThatBai)
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_XOA, ENTITY_NAME.CHUYENNGANHNCS));
                }
            }
        }

        private void FormQuanLyNganhDaoTao_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (reload == true)
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            else
                this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
    }
}
